package com.appcenter.message.mapper;

import com.appcenter.message.input.GetUnreadMessageCountInput;
import com.appcenter.message.input.SearchMessageListInput;
import com.appcenter.message.model.MessageTargets;
import org.apache.ibatis.annotations.*;
import org.hibernate.validator.constraints.EAN;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface MessageTargetsMapper extends tk.mybatis.mapper.common.Mapper<MessageTargets> {


    @Insert("<script>" +
            "insert into messagetargets(Id,MessageId,TargetUserName,TargetUserCode,IsRead,IsSend,ReadTime,CreateTime,SendTime)" +
            "values(#{Id},#{MessageId},#{TargetUserName},#{TargetUserCode},#{IsRead},#{IsSend},#{ReadTime},#{CreateTime},#{SendTime})" +
            "</script>")
    int addMessageTargets(MessageTargets MessageTarget);

    @Select("select * from messagetargets where Id=#{MsgTagerId}")
    MessageTargets getMessageTargets(String MsgTagerId);

    @Select("<script>" +
            "select messagetargets.* from messagetargets inner JOIN messages on messages.Id=MessageId" +
            " where 1=1" +
            " <when test='msgStart!=null'>" +
            " and messagetargets.IsRead=#{msgStart}" +
            " </when>" +
            "" +
            " <when test='businessType!=null'>" +
            " AND messages.BusinessType=#{businessType}" +
            " </when>" +
            "" +
            " <when test='appCode!=null'>" +
            " AND messages.FromAppCode=#{appCode}" +
            " </when>" +
            "" +
            " <when test='userId!=null'>" +
            " AND messagetargets.TargetUserCode=#{userId}" +
            " </when>" +
            "" +
            " and messages.MessageType='站内信'" +
            " and messagetargets.IsSend=1" +
            "" +
            "</script>")
    List<MessageTargets> getUnreadMessageCount(GetUnreadMessageCountInput input);


    @Results({
            @Result(column="MessageId",property="messages",one=@One(select="com.appcenter.message.mapper.MessagesMapper.getMessage"))})
    @Select("<script>" +
            "select a.* from messagetargets a inner join messages b on b.Id=a.MessageId" +
            " where 1=1 " +
            "" +
            " <when test='keyword!=null'>" +
            " AND (b.Title like concat('%',#{keyword},'%') or MsContent like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            " <when test='msgStart!=null'>" +
            " and a.IsRead=#{msgStart}" +
            " </when>" +
            "" +
            " <when test='businessType!=null'>" +
            " and BusinessType=#{BusinessType}" +
           " </when>" +
            "" +
            " <when test='appCode!=null'>" +
           " and FromAppCode=#{appCode}" +
            " </when>" +
            "" +
            " <when test='userId!=null'>" +
            " and TargetUserCode=#{userId}" +
            " </when>" +
            "" +
            " and MessageType='站内信' and a.IsSend=1" +
            "</script>")
    List<MessageTargets> GetAllMessageTargets(SearchMessageListInput input);

}
